package com.yz.jdd.network;


import com.yz.jdd.base.entity.Response_Base;
import com.yz.jdd.base.utils.Tools;

import rx.Subscriber;

/**
 * Created by shibin on 2016/12/25.
 */
public abstract class ApiCallback<T> extends Subscriber {
    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onCompleted() {
    }

    @Override
    public void onError(Throwable e) {
//        LogBackUtil.e( e.getMessage());
        Response_Base<T> response_base = new Response_Base<>();
        response_base.setMsg("网络忙,请稍后重试...");
        response_base.setCode("-1");
        onFail(response_base);
    }

    @Override
    public void onNext(Object o) {

        Response_Base<T> response_baseModel = (Response_Base<T>) o;
        if (response_baseModel != null) {
            if (response_baseModel.success()) {
                onSuccess(response_baseModel);
            } else if ("302".equals(response_baseModel.getCode())) {

                onNeedLogin(Tools.getSafeString(response_baseModel.getMsg()));
            }else if("1502".equals(response_baseModel.getCode())){
                //"session已经登录"不做拦截
                onSuccess(response_baseModel);
            }
            else {
                onFail(response_baseModel);
            }
        } else {
            response_baseModel = new Response_Base<>();
            response_baseModel.setMsg("response is null exception");
            onFail(response_baseModel);
        }
    }

    public abstract void onSuccess(Response_Base<T> response_baseModel);

    public abstract void onNeedLogin(String msg);

    public abstract void onFail(Response_Base<T> response_baseModel);
}
